package com.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.app.util.Utils_Log;
import com.app.util.cHighTimer;

import lib.common.R;


public class ViewDymic extends View
{

	private static String TAG = ViewDymic.class.getSimpleName();

	private Context mContext;


	private Paint mPaintLine;
	private Point point_line_start = new Point();
	private Point point_line_end = new Point();

	private Point point_line_start1 = new Point();
	private Point point_line_end1 = new Point();
	private int mColorLine = Color.BLACK;


	int width;
	int height;



	///*
	public ViewDymic(Context context, AttributeSet set){
		super(context, set);
		
		init(context, set, 0);
		
	}
	//*/


	private void init(Context context, AttributeSet attrs, int defStyle) {
		// Load attributes


		mContext = context;


		if (null != attrs) {
			TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyCircleSurfaceView);

			if (ta != null) {

				ta.recycle();
			}
		} else {


		}

		//  这个时候位置和尺寸还不确定
		initAnimation();

	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);


		update();

	//	Utils_Log.e(TAG, "width: " + width + " height: " + height + " x: " + x + " y: " + y);



		//canvas.drawColor(Color.BLUE);
		//canvas.drawCircle(width / 2, height / 2, 30, mPaintLine);
//		canvas.drawLine(point_line_start.x, point_line_start.y, point_line_end.x, point_line_end.y, mPaintLine);

		canvas.drawColor(Color.TRANSPARENT);


		//
		canvas.drawLine(point_line_start1.x, point_line_start1.y, point_line_end1.x, point_line_end1.y, mPaintLine);


		/*
		postInvalidateDelayed(40,
				point_line_start.x - 6,
				point_line_start.y - 6,
				point_line_end.x + 6,
				point_line_end.y + 6);
		*/

		postInvalidateDelayed(40);



	}


	void initAnimation(){


		mPaintLine = new Paint();
		mPaintLine.setColor( mColorLine );
		mPaintLine.setStrokeWidth(5);


	}
	
	int x;
	int y;
	int delta_y = 0;
	boolean bFirst = true;
	void update(){

		if(bFirst){
			x = getLeft();
			y = getTop();
			width = getWidth();
			height = getHeight();


			point_line_start.x = x;
			point_line_start.y = y;
			point_line_end.x = x+width;
			point_line_end.y = y;


			point_line_start1.x = x;
			point_line_start1.y = y;
			point_line_end1.x = x;
			point_line_end1.y = y;

			bFirst = false;
		}


		if(point_line_start.y>getBottom()){
			y  = getTop();
			point_line_start.y = y;
			point_line_end.y = y;
		}

		point_line_start.offset(0, 1);
		point_line_end.offset(0, 1);


		if(bZero){
			do_zero();
		}
		else if(bOne){
			do_one();
		}
		else if(bTwo){
			do_two();
		}
		else if(bThree){
			do_three();
		}


		//Utils_Log.d(TAG, "point_line_start: " + point_line_start.toString());

	}


	// 垂直向下
	void do_zero(){

		if(point_line_end1.y>=getBottom()){
			bZero = false;
			bOne = true;
			point_line_start1.set(getLeft(), getBottom());

		}
		else{
			point_line_end1.offset(0, speed);
		}
	}

	// 左下角到右上角
	void do_one(){
		if(point_line_end1.y<=getTop()){
			bOne = false;
			bTwo = true;
			point_line_start1.set(getRight()-5, getTop());
			point_line_end1.set(point_line_start1.x, point_line_start1.y);  // 起点和终点移动到右上角
		}
		else{
			point_line_end1.offset(speed,-speed);
		}


	}

	// 右上角到右下角
	void do_two(){
		if(point_line_end1.y>=getBottom()){
			bTwo = false;
			bThree = true;
			point_line_start1.set(getRight(), getBottom());
			point_line_end1.set(point_line_start1.x, point_line_start1.y);
		}
		else{
			point_line_end1.offset(0, speed);
		}
	}

	void do_three(){
		if(point_line_end1.y<=getTop()){
			bZero = true;
			bThree = false;
			point_line_start1.set(getLeft(), getTop());
			point_line_end1.set(point_line_start1.x, point_line_start1.y);

		}
		else{
			point_line_end1.offset(-speed, -speed);
		}
	}
	

	int speed = 40;


	boolean bZero = true;
	boolean bOne = false;
	boolean bTwo = false;
	boolean bThree = false;






};

